package com.chengliang.zuoye.controller;

import com.chengliang.zuoye.bean.Role;
import com.chengliang.zuoye.response.JsonResult;
import com.chengliang.zuoye.service.MenuService;
import com.chengliang.zuoye.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author chengliang
 * @Date 2019-06-15 15:58
 * @Description //TODO
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    private Logger logger= LoggerFactory.getLogger(getClass());

    private final RoleService roleService;

    private final MenuService menuService;

    @Autowired
    public RoleController(RoleService roleService, MenuService menuService) {
        this.roleService = roleService;
        this.menuService = menuService;
    }


    @PostMapping("/add")
    @ResponseBody
    public Object add(Role role,@RequestParam("ids[]") List<Integer> menuIds){
        return JsonResult.is(roleService.saveRole(role,menuIds),"添加");
    }
    /**
     * 添加页面
     */
    @GetMapping("/add")
    public String addPage(){
        return "role/add";
    }

    @PostMapping("/delete")
    @ResponseBody
    public Object deleteMany(@RequestParam("ids[]") List<Integer> ids){
        logger.info("角色批量删除"+ids);
        return JsonResult.is(roleService.deleteRoleByIds(ids),"角色批量删除");
    }

    @PostMapping("/delete/{roleId}")
    @ResponseBody
    public Object delete(@PathVariable Integer roleId){
        return JsonResult.is(roleService.deleteRoleById(roleId),"删除");
    }

    /**
     * 角色管理首页
     */
    @GetMapping("/index")
    public String index(){
        return "role/index";
    }

    /**
     * 角色json数据
     */
    @GetMapping("/data/json")
    @ResponseBody
    public Object dataJson(){
        return JsonResult.ok(roleService.roleList());
    }

}
